package com.gxuwz.com_property.arrears.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.com_property.arrears.dto.ArrearsDto;
import com.gxuwz.com_property.arrears.mapper.ArrearsMapper;
import com.gxuwz.com_property.arrears.pojo.Arrears;
import com.gxuwz.com_property.arrears.service.ArrearsService;
import com.gxuwz.com_property.business.mapper.BusinessMapper;
import com.gxuwz.com_property.business.pojo.Business;
import com.gxuwz.com_property.user.mapper.UserMapper;
import com.gxuwz.com_property.utils.RamdomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class ArrearsServiceImpl implements ArrearsService {

    @Autowired
    private ArrearsMapper arrearsMapper;

    @Autowired
    private BusinessMapper businessMapper;


    @Autowired
    private RamdomUtils ramdomUtils;

    @Autowired
    private Business business;

    @Override
    public Boolean addArrearsInfo(Arrears arrears) {
        Boolean aBoolean = arrearsMapper.addArrearsInfo(arrears);
        return aBoolean;
    }

    @Override
    public Boolean updateArrearsInfo(Arrears arrears) {
        String address = arrears.getAddress();

        if (arrears.getStatus().equals("已缴费")){
            if (address!=null || address!=""){
                /**
                 * 线下缴费
                 * */
                LocalDateTime now = LocalDateTime.now(); // 获取当前时间
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 定义时间格式
                String mysqlDateTime = dtf.format(now); // 将LocalDateTime对象转换为符合MySQL的日期时间格式
                business.setPaymentDate(mysqlDateTime);

                business.setAmount(arrears.getAmount());
                business.setContent(arrears.getFeeType());
                business.setPayType("现金");
                long repair_id = ramdomUtils.generateId();
                business.setId(repair_id);
                business.setPayUser(arrears.getAddress()+"的业主");

                boolean b = businessMapper.addBusinessInfo(business);
                if (!b){
                    return false;
                }
            }
        }

        Boolean aBoolean = arrearsMapper.updateArrearsInfo(arrears);
        return aBoolean;
    }

    @Override
    public Boolean deleteArrearsByIdInfo(long id) {
        Boolean aBoolean = arrearsMapper.deleteArrearsByIdInfo(id);
        return aBoolean;
    }

    @Override
    public Arrears findArrearsByNotice(Arrears arrears) {
        Arrears arrearsByNotice = arrearsMapper.findArrearsByNotice(arrears);
        return arrearsByNotice;
    }

    @Override
    public IPage<Arrears> selectArrearsByPage(Page<Arrears> page, ArrearsDto arrearsDto) {
        IPage<Arrears> arrearsIPage = arrearsMapper.selectArrearsByPage(page, arrearsDto);
        return arrearsIPage;
    }
}
